package com.atguigu.lease.web.admin.service.imp;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    RoomLabelMapper roomLabelMapper;

    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public IPage<RoomItemVo> selectRoomPage(IPage<RoomItemVo> roomItemVoIPage, RoomQueryVo queryVo) {

        return roomInfoMapper.selectRoomList(roomItemVoIPage,queryVo);
    }

    @Transactional
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        Long id = roomSubmitVo.getId();

        if(id != null){
            //如果有存在id先删除 ，表示更新操作
            System.out.println("id = " + id);

            //图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

            //属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
            roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);

            //配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper =new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
            roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);

            //标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper =new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
            roomLabelMapper.delete(roomLabelLambdaQueryWrapper);

            //支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper =new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
            roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
            //可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper =new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
            roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);
        }

        //自己的数据
        this.saveOrUpdate(roomSubmitVo);
         id = roomSubmitVo.getId();

        //图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        graphInfoMapper.insertBatch(2, id, graphVoList);
        //属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        roomAttrValueMapper.insertBatch(id,attrValueIds);
        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        roomFacilityMapper.insertBatch(id,facilityInfoIds);
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        roomLabelMapper.insertBatch(id,labelInfoIds);
        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        roomPaymentTypeMapper.insertBatch(id,paymentTypeIds);
        //可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        roomLeaseTermMapper.insertBatch(id,leaseTermIds);

    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo =new RoomDetailVo();

        RoomInfo roomInfo = this.getById(id);
        //设置房间信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //设置房间属性
        List<AttrValueVo>  attrValueVoList = roomAttrValueMapper.selectByIdRoom(id);

        //设置房间配套
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectByIdRoom(id);

        //设置放假标签
        List<LabelInfo> labelInfoList =roomLabelMapper.selectByIdRoom(id);

        //设置支付方式
        List<PaymentType> paymentTypeList =roomPaymentTypeMapper.selectByIdRoom(id);

        //设置可选租期
        List<LeaseTerm> leaseTermList =roomLeaseTermMapper.selectByIdRoom(id);

        //设置图片  没有连表 可以直接获取
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo =new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        }


        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setGraphVoList(graphVoList);
        return roomDetailVo;
    }

    @Transactional
    @Override
    public void removeByIdRoom(Long id) {

        //图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        //属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);

        //配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);

        //标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelMapper.delete(roomLabelLambdaQueryWrapper);

        //支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
        //可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);

        //删除room信息储存表
        this.removeById(id);
    }
}




